package com.uusee.crawler.job.baike;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.uusee.crawler.dbwriter.BaikeDataSourceDbWriter;
import com.uusee.crawler.dbwriter.BaikeDbWriter;
import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.framework.AbstractJob;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.baike.BaikeDataSourcePageProcessor;
import com.uusee.crawler.pageprocessor.baike.BaikePageProcessor;
import com.uusee.crawler.pageprocessor.baike.xunlei.XunleiMoviePlotsPageProcessor;
import com.uusee.crawler.xmlwriter.BaikeXmlWriter;
import com.uusee.framework.bo.UniversalBo;
import com.uusee.shipshape.bk.model.Baike;
import com.uusee.shipshape.bk.model.BaikeDataSource;

public class XunLeiBaikeFastCrawlJob extends AbstractJob {
	private static Log log = LogFactory.getLog(XunLeiBaikeFastCrawlJob.class);
	private FetchHTTP fetch = new FetchHTTP();
	private BaikeDataSourcePageProcessor overviewPageProcessor;
	private XunleiMoviePlotsPageProcessor plotsPageProcessor = new XunleiMoviePlotsPageProcessor();
	private BaikeDataSourceDbWriter dbWriter = new BaikeDataSourceDbWriter();

	public CrawlURI createCrawlURI(String oriId) {
		CrawlURI crawlURI = new CrawlURI();
		String crawlUrl = "http://data.movie.xunlei.com/movie/" + oriId;
		crawlURI.setSourceSite("www.xunlei.com");
		crawlURI.setCrawlUrl(crawlUrl);
		return crawlURI;
	}
	
	public void crawlOnPage(CrawlURI crawlURI) {
		try {
			Baike baike = (Baike) crawlURI.getModel();
			String oriId = baike.getOriId();

			// 抓取概览数据
			fetch.process(crawlURI);
			overviewPageProcessor.process(crawlURI);

			if (crawlURI.getCrawlStatus() == CrawlStatusCodes.PAGE_PROCESS_SUCCESS) {
				// 抓取剧情
				String crawlUrl = "http://movie.xunlei.com/movie/" + oriId + "/introduction";
				crawlURI.setCrawlUrl(crawlUrl);
				crawlURI.setCrawlStatus(CrawlStatusCodes.DEFAULT_STATUS);

				fetch.process(crawlURI);
				plotsPageProcessor.process(crawlURI);
			}else {
				crawlURI.setModel(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
	}

	public void crawl(CrawlURI crawlURI) {
		try {
			fetch.process(crawlURI);
			overviewPageProcessor.process(crawlURI);
			if (crawlURI.getCrawlStatus() == CrawlStatusCodes.PAGE_PROCESS_SUCCESS) {
				BaikeDataSource bds = (BaikeDataSource)crawlURI.getModel();
				// 抓取剧情
				String crawlUrl = "http://movie.xunlei.com/movie/" + bds.getOriId() + "/introduction";
				crawlURI.setCrawlUrl(crawlUrl);
				crawlURI.setCrawlStatus(CrawlStatusCodes.DEFAULT_STATUS);
				fetch.process(crawlURI);
				plotsPageProcessor.process(crawlURI);
				dbWriter.process(crawlURI);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
	}

	public static void main(String[] args) {
		try {
			
			ApplicationContext acx = new ClassPathXmlApplicationContext("applicationContext.xml");
			UniversalBo universalBo = (UniversalBo) acx.getBean("universalBo");
			BaikeDataSourceDbWriter dbWriter = new BaikeDataSourceDbWriter();
			dbWriter.setUniversalBo(universalBo);
			
			InputStream in = ClassLoader.getSystemResourceAsStream("regex/www.xunlei.com/overview.properties");
			Properties processPageRegex = new Properties();
			processPageRegex.load(in);
			BaikeDataSourcePageProcessor pageProcessor = new BaikeDataSourcePageProcessor(processPageRegex);
			
			XunLeiBaikeFastCrawlJob job = new XunLeiBaikeFastCrawlJob();
			job.dbWriter = dbWriter;
			job.overviewPageProcessor = pageProcessor;
			//finished:0-50000
			job.setStartVid(50000);
			job.setEndVid(60000);
			job.setMultiThreadFlag(true);
			job.doExecute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
